package com.jing.lock;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 线程通信
 */

class ShareDataOne{
    //private int number = 0;

/*    public synchronized void increment(){
        //判断
        if (number != 0) {
            try {
                wait();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
        //干活
        System.out.println(Thread.currentThread().getName() + "\t" + (++number));
        //通知
        notifyAll();
    }
*/
    /*public synchronized void increment(){
        //判断
        while (number != 0) {
            try {
                wait();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
        //干活
        System.out.println(Thread.currentThread().getName() + "\t" + (++number));
        //通知
        notifyAll();
    }*/

/*    public synchronized void decrement(){
        if (number == 0){
            try {
                wait();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
        System.out.println(Thread.currentThread().getName() + "\t" + (--number));
        notifyAll();
    }
*/
        /*public synchronized void decrement(){
            while (number == 0){
                try {
                    wait();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
            System.out.println(Thread.currentThread().getName() + "\t" + (--number));
            notifyAll();
        }*/


    //===================JUC新版

    private  int number = 0;
    Lock lock = new ReentrantLock();
    Condition condition = lock.newCondition();
    public void increment(){
        lock.lock();
        try {
            while (number != 0){
                condition.await();
            }
            System.out.println(Thread.currentThread().getName() + "\t" + (++number));
            condition.signalAll();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        } finally {
            lock.unlock();
        }
    }

    public void decrement(){
        lock.lock();
        try {
            while (number == 0){
                condition.await();
            }
            System.out.println(Thread.currentThread().getName() + "\t" + (--number));
            condition.signalAll();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        } finally {
            lock.unlock();
        }
    }
}


public class ProdConsumerDemo {
    public static void main(String[] args) {
        ShareDataOne one = new ShareDataOne();
        new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                one.increment();
            }
        },"A").start();

        new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                one.decrement();
            }
        },"B").start();
        new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                one.increment();
            }
        },"C").start();

        new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                one.decrement();
            }
        },"D").start();
    }
}
